// pages/exchangegold/exchangegold.js
var app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allGoodsorder:[],
    total:0
  },
  exchange:function(){
    var that = this;
    var back_ids=[];
    var back_idstring = app.backpack_record.backpack_order;
    for (var i = 0; i < back_idstring.length;i++){
      back_ids.push(parseInt(back_idstring[i]))
    }
    console.log(back_ids);
    wx.request({
      method: 'GET',
      url: app.globalData.api_url + "/OpenAPI/V1/User/backpackExchange",
      data:{token:app.user.token,toy_ids:back_ids,iscx:1},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        wx.navigateBack();
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.api_url + "/OpenAPI/V1/Backpack/getAll",
      data: {
        token: app.user.token,
        page: 1
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var list = res.data.data.list;
        var backpack_order = app.backpack_record.backpack_order;
        var backpackArr = [];
        for (var i = 0; i < list.length; i++) {
          list[i]['num'] = 1;
          if (backpack_order.indexOf(list[i]['backpack_id']) >= 0) {
            backpackArr.push(list[i]);
            that.setData({
              allGoodsorder: backpackArr
            })
          }
        }
        var total_num=0;
        for (var i = 0; i < backpackArr.length;i++){
          total_num += parseInt(backpackArr[i]['to_user_price']);
        }
        that.setData({
          total: total_num
        })
        console.log(backpackArr, total_num)
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})